/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.ejb;

import entities.Fantasyleague;
import entities.Fantasyteam;
import entities.Fantasyuser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author The Lunatics
 */
@Stateless
public class FantasyteamFacade extends AbstractFacade<Fantasyteam> {

    @PersistenceContext(unitName = "Touchdown308PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public FantasyteamFacade() {
        super(Fantasyteam.class);
    }

    public List<Fantasyteam> getUsersTeams(Fantasyuser id) {
        Query createQuery = getEntityManager().createQuery("select f from Fantasyteam f where f.userId = :userId");
        createQuery.setParameter("userId", id);

        List<Fantasyteam> l = createQuery.getResultList();

        return l;
    }

    public Fantasyteam getTeamX(Fantasyleague leagueId, int x, int max) {
        Query createQuery = getEntityManager().createQuery("select f from Fantasyteam f where f.leagueId = :leagueId");
        createQuery.setParameter("leagueId", leagueId);

        List<Fantasyteam> l = createQuery.getResultList();

        if (x <= l.size()) {
            return l.get(x - 1);
        } else {
            return null;
        }

    }

    public Fantasyteam getTeamId(Fantasyleague l, Fantasyuser u) {
        Query createQuery = getEntityManager().createQuery("select f from Fantasyteam f where f.leagueId = :leagueId and f.userId = :userId");
        createQuery.setParameter("leagueId", l);
        createQuery.setParameter("userId", u);

        if (createQuery.getResultList().size() > 0) {
            return (Fantasyteam) createQuery.getSingleResult();
        } else {
            return null;
        }

    }

    public Fantasyteam getTeamById(String s) {
        Integer id = new Integer(s);

        Query createNamedQuery = getEntityManager()
                .createNamedQuery("Fantasyteam.findById");

        createNamedQuery.setParameter("id", id);

        if (createNamedQuery.getResultList()
                .size() > 0) {
            return (Fantasyteam) createNamedQuery.getSingleResult();
        } else {
            return null;
        }

    }

    public List<Fantasyteam> findFantasyTeamByUser(Fantasyuser user) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        Query q = getEntityManager().createNamedQuery("Fantasyteam.findByUserId");
        q.setParameter("userId", user);
        return q.getResultList();
    }

    public void inviteUser(Fantasyteam team) {
        System.out.println(team);
        getEntityManager().persist(team);
    }

    public List<Fantasyteam> getAllTeams(Fantasyleague l) {
        Query createQuery = getEntityManager().createQuery("select f from Fantasyteam f where f.leagueId = :leagueId");
        createQuery.setParameter("leagueId", l);

        return createQuery.getResultList();
    }
}
